package com.bupt.kgplatform.controller;


import com.bupt.kgplatform.common.RetResult;
import com.bupt.kgplatform.common.TugraphUtil;
import com.bupt.kgplatform.entity.KgDatabase;
import com.bupt.kgplatform.entity.apibo.KGDBBO;
import com.bupt.kgplatform.service.KgDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/kgplatform/kgdatasets")
public class DataBaseController {

    @Autowired
    KgDatabaseService kgdatabaseService;

    @GetMapping("getDatasets")
    public RetResult getDatasets() {
        List<KGDBBO> res = new ArrayList<>();
        List<KgDatabase> temp;
        try{
            temp = kgdatabaseService.list();
            for(KgDatabase d : temp){
                res.add(new KGDBBO(d));
            }
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200, "查询成功", res);
    }

    @PostMapping("newDataset")
    public RetResult newDataset(@RequestParam String dbname, @RequestParam String database, @RequestParam String url, @RequestParam String port,
                                @RequestParam String username, @RequestParam String password, @RequestParam String reviser) {
        try{
            TugraphUtil.addSubGraph(dbname, dbname+"-D2G转化图谱");
            kgdatabaseService.newDB(dbname, database, url, port, username, password, reviser);
            return new RetResult(200, "创建成功,图谱初始化");
        }
        catch (Exception e) {
            return new RetResult(400, dbname+"图谱已存在，请检查名称");
        }
    }

    @PostMapping("deleteDataset")
    public RetResult deleteDataset(@RequestParam String db_number) {
        try{
            kgdatabaseService.deleteDB(db_number);
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200, "删除成功");
    }

    @PostMapping("changeSource")
    public RetResult changeSource(@RequestParam String dbnumber) {
        try{
            kgdatabaseService.changeSource(dbnumber);
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200, "连接数据库成功");
    }

    @PostMapping("getdbs")
    public RetResult getdbs(@RequestParam String dbnumber) {
        List res;
        try{
            res = kgdatabaseService.getdbs(dbnumber);
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200, "查询全部数据库成功", res);
    }

    @PostMapping("addVLSchemas")
    public RetResult addVLSchemas(@RequestParam String entity, @RequestParam String dbnumber,@RequestParam String dsname) {
        try {
            if(kgdatabaseService.addVLSchema(entity, dbnumber,dsname))
                return new RetResult(200, "转换中");
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(400, "转换失败");
    }

    @PostMapping("addELSchemas")
    public RetResult addELSchemas(@RequestParam String relation, @RequestParam String dbnumber,@RequestParam String dsname) {
        try {
            if(kgdatabaseService.addELSchema(relation, dbnumber,dsname))
                return new RetResult(200, "转换中");
        }
        catch (Exception e) {
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(400, "转换失败");
    }

    //测试用 无意义
    @PostMapping("/test1")
    public RetResult test1(@RequestParam String dbnumber){
        try {
            kgdatabaseService.changeSource(dbnumber);
        }
        catch (Exception e){
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200,"添加成功");
    }

    @PostMapping("/test2")
    public RetResult test2(@RequestParam String dbnumber){
        List res;
        try {
            res = kgdatabaseService.getdbs(dbnumber);
        }
        catch (Exception e){
            return new RetResult(400, e.getMessage());
        }
        return new RetResult(200,"查询全部数据库成功", res);
    }
}
